package cn.portx.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import cn.portx.entity.Car;
import cn.portx.entity.Fleet;

@Component
public class CarPR {

	@DataProvider
	@Transactional(readOnly = true)
	public void loadCars(Page<Car> page, Criteria criteria) {
		JpaUtil.linq(Car.class).collect(Fleet.class, "fleetId").where(criteria).paging(page);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public void loadFleets(Page<Fleet> page, Map<String, Object> params) {
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		String filterValue = (String) params.get("filterValue");
		JpaUtil.linq(Fleet.class).addIf(filterValue).or().like("name", filterValue + "%")
				.like("code", filterValue + "%").end().endIf().paging(page);
	}

	@DataResolver
	@Transactional
	public void saveCars(List<Car> list) {
		JpaUtil.save(list);
	}
}
